package region

import (
	"context"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/api/internal/utils"
	"mall/service/backend/common/i18n"
	"mall/service/setting/rpc/settingclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRegionByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetRegionByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRegionByIdLogic {
	return &GetRegionByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetRegionByIdLogic) GetRegionById(req *types.IDReq) (resp *types.RegionInfoResp, err error) {
	data, err := l.svcCtx.SettingRpc.BackendGetRegionById(l.ctx, &settingclient.IDReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}

	option := utils.GetOptionByStatus(data.Status)

	return &types.RegionInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.RegionInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         data.Id,
				CreatedAt:  data.CreatedAt,
				UpdatedAt:  data.UpdatedAt,
				DeletedAt:  data.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			ParentId: data.ParentId,
			Level:    data.Level,
			Sort:     data.Sort,
			Name:     data.Name,
			Code:     data.Code,
			Region:   data.Region,
			Status:   data.Status,
		},
	}, nil
}
